/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package introse;

import java.util.List;

/**
 *
 * @author Rommel
 */
public class productViewer extends javax.swing.JPanel {

    /**
     * Creates new form productManager
     */
    private productManager productMngr;
    private uneditableTableModel productTableModel;

    public productViewer() {
        initComponents();
    }

    public void loadProductsTable() {
        //Reset Product List And Table
        productMngr = new productManager();
        productTableModel = new uneditableTableModel(new String[]{"Product Number", "Stock Add", "Stock Less", "isActive", "Product Name", "Product Type", "Price", "Stock"});
        productTable.setModel(productTableModel);

        //Hide first 4 columns (Product Number, Stock Add, Stock Less, isActive)
        productTable.removeColumn(productTable.getColumnModel().getColumn(0));
        productTable.removeColumn(productTable.getColumnModel().getColumn(0));
        productTable.removeColumn(productTable.getColumnModel().getColumn(0));
        productTable.removeColumn(productTable.getColumnModel().getColumn(0));

        //Load products from productManager to table
     /*
         * regenerated by the Form Editor.
         */
        for (product prod : productMngr.getProducts(true)) {
            productTableModel.addRow(new String[]{Integer.toString(prod.getProductNumber()), Integer.toString(prod.getStockAdd()), Integer.toString(prod.getStockMinus()), Boolean.toString(prod.getIsActive()), prod.getProductName(), prod.getIsGasul() ? "Gasul" : "Miscellaneous", Float.toString(prod.getPrice()) + " PHP", Integer.toString(prod.getStock() + prod.getStockAdd() - prod.getStockMinus())});
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        productTable = new javax.swing.JTable();
        addProductButton = new javax.swing.JButton();
        deactivateProductButton = new javax.swing.JButton();
        editProductButton = new javax.swing.JButton();
        viewDailyFilledInventoryReportButton = new javax.swing.JButton();
        reactivateButtonPressed = new javax.swing.JButton();

        productTable.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        productTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(productTable);

        addProductButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Product Add.png"))); // NOI18N
        addProductButton.setText("Add Product");
        addProductButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addProductButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        addProductButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addProductButtonActionPerformed(evt);
            }
        });

        deactivateProductButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Product Minus.png"))); // NOI18N
        deactivateProductButton.setText("Deactivate Product");
        deactivateProductButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deactivateProductButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        deactivateProductButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deactivateProductButtonActionPerformed(evt);
            }
        });

        editProductButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Product Edit.png"))); // NOI18N
        editProductButton.setText("Edit Product");
        editProductButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editProductButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        editProductButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editProductButtonActionPerformed(evt);
            }
        });

        viewDailyFilledInventoryReportButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Generate Report.png"))); // NOI18N
        viewDailyFilledInventoryReportButton.setText("<html>\n<p align = center>\nView Daily <br>\nFilled Inventoy <br>\nReport\n</p>\n</html>");
        viewDailyFilledInventoryReportButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        viewDailyFilledInventoryReportButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        viewDailyFilledInventoryReportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewDailyFilledInventoryReportButtonActionPerformed(evt);
            }
        });

        reactivateButtonPressed.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Revert Product.png"))); // NOI18N
        reactivateButtonPressed.setText("Reactivate Products");
        reactivateButtonPressed.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        reactivateButtonPressed.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        reactivateButtonPressed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reactivateButtonPressedActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 840, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(viewDailyFilledInventoryReportButton)
                    .addComponent(reactivateButtonPressed, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(editProductButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(addProductButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(deactivateProductButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addProductButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editProductButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deactivateProductButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(reactivateButtonPressed)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(viewDailyFilledInventoryReportButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addProductButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addProductButtonActionPerformed
        new addProduct(null, true).setVisible(true);
        loadProductsTable();
    }//GEN-LAST:event_addProductButtonActionPerformed

    private void editProductButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editProductButtonActionPerformed
        try {
            int productNumber = Integer.parseInt(productTableModel.getValueAt(productTable.getSelectedRow(), 0).toString());
            if (productNumber > 0) {
                editProduct editProd = new editProduct(null, true);
                editProd.setProduct(productMngr.getProduct(Integer.parseInt(productTableModel.getValueAt(productTable.getSelectedRow(), 0).toString())));
                editProd.setVisible(true);
            }
        } catch (Exception ex) {
            errorDialogue errorMessage = new errorDialogue(null, true);
            errorMessage.setMessage("No product selected");
            errorMessage.setVisible(true);
        }
        loadProductsTable();
    }//GEN-LAST:event_editProductButtonActionPerformed

    private void deactivateProductButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deactivateProductButtonActionPerformed
        try {
            int productNumber = Integer.parseInt(productTableModel.getValueAt(productTable.getSelectedRow(), 0).toString());
            deactivationDialog delDiag = new deactivationDialog(null, true);
            delDiag.setVisible(true);
            if (delDiag.getReturnStatus() == 1) {
                productMngr.getProduct(productNumber).setIsActive(false);
                positiveMessage posMsg = new positiveMessage(null, true);
                posMsg.setMessage("Product has been deactivated");
                posMsg.setVisible(true);
            }
        } catch (Exception ex) {
            errorDialogue errorMessage = new errorDialogue(null, true);
            errorMessage.setMessage("No product selected");
            errorMessage.setVisible(true);
        }
        loadProductsTable();
    }//GEN-LAST:event_deactivateProductButtonActionPerformed

    private void viewDailyFilledInventoryReportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewDailyFilledInventoryReportButtonActionPerformed
        new dailyFilledInventoryReport(null, true).setVisible(true);
    }//GEN-LAST:event_viewDailyFilledInventoryReportButtonActionPerformed

    private void reactivateButtonPressedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reactivateButtonPressedActionPerformed
        reactivateProducts reactProds = new reactivateProducts(null, true);
        reactProds.setVisible(true);
        loadProductsTable();
    }//GEN-LAST:event_reactivateButtonPressedActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addProductButton;
    private javax.swing.JButton deactivateProductButton;
    private javax.swing.JButton editProductButton;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable productTable;
    private javax.swing.JButton reactivateButtonPressed;
    private javax.swing.JButton viewDailyFilledInventoryReportButton;
    // End of variables declaration//GEN-END:variables
}
